package com.carsite.dao.hibernate;

import java.util.List;

import com.carsite.dao.RoleDao;
import com.carsite.model.QueryMap;
import com.carsite.model.Role;



/**
 * This class interacts with Spring's HibernateTemplate to save/delete and
 * retrieve Role objects.
 *
 * <p>
 * <a href="RoleDaoHibernate.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author <a href="mailto:dan@getrolling.com">Dan Kibler</a> 
 */
public class RoleDaoHibernate extends BaseDaoHibernate implements RoleDao {
   
	
   
    public List getRoles(Role role) {
        return getHibernateTemplate().find("from Role");
    }
    
    public Role getRole(Long roleId) {
        return (Role) getHibernateTemplate().get(Role.class, roleId);
     
    }

    public Role getRoleByName(String rolename) {
        List roles = getHibernateTemplate().find("from Role where name=?", rolename);
        if (roles.isEmpty()) {
            return null;
        } else {
            return (Role) roles.get(0);
        }
    }

    public void saveRole(Role role) {
        getHibernateTemplate().saveOrUpdate(role);
    }

    public void removeRole(String rolename) {
        Object role = getRoleByName(rolename);
        getHibernateTemplate().delete(role);
    }
    
  
  
    //added
    public void removeRole(final Long id) {
        getHibernateTemplate().delete(getRole(id));
    } 
    
    public Role getRoleById(Long roleId){
    	List roles = getHibernateTemplate().find("from Role where id=?", roleId);
        if (roles.isEmpty()) {
            return null;
        } else {
            return (Role) roles.get(0);
        }
    }
    public List getRolesByCondition(String roleCondition){
    	String strSQL="from Role where levels like '%"+roleCondition+"%'";
    	return getHibernateTemplate().find(strSQL);
        
    }

	/* (non-Javadoc)
	 * @see com.clothserver.dao.RoleDao#getRolesByLevel(java.lang.String)
	 */
	public List getRolesByLevel(String level) {
		 return getHibernateTemplate().find("from Role where levels like '%"+level+"%'");
	}

	/* (non-Javadoc)
	 * @see com.clothserver.dao.RoleDao#getRoleByConditions(com.clothserver.model.QueryMap)
	 */
	public List getRoleByConditions(QueryMap queryMap) {
	
		String sql = queryMap.getQuerySql();
		List list = getHibernateTemplate().find("from Role "+sql);
		return list;
	}
   
}
